<template>
  <div class="heroEdit">
    <h1>{{id?'编辑':'新建'}}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs type="border-card">
        <el-tab-pane label="基础信息">
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL+'admin/upload'"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="afterUpload"
            >
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="背景">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL+'admin/upload'"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="afterUpload2"
            >
              <img v-if="model.bgc" :src="model.bgc" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="英雄类型">
            <el-select v-model="model.type" multiple>
              <el-option
                v-for="(item,index) in  types"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate style="margin-top:0.6rem" :max="10" show-score v-model="model.score.difficult"></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate style="margin-top:0.6rem" :max="10" show-score v-model="model.score.skills"></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate style="margin-top:0.6rem" :max="10" show-score v-model="model.score.attack"></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate style="margin-top:0.6rem" :max="10" show-score v-model="model.score.survival"></el-rate>
          </el-form-item>
          <el-form-item label="推荐装备">
            <el-select v-model="model.heroitem" multiple>
              <el-option v-for="(i,index) in  item" :key="index" :label="i.name" :value="i.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input v-model="model.tip.usageTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input v-model="model.tip.battleTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input v-model="model.tip.teamTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="最佳搭档1">
            <el-input v-model="model.cooperates[0].hero"></el-input>
          </el-form-item>
          <el-form-item label="最佳搭档的原因">
            <el-input v-model="model.cooperates[0].description" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="最佳搭档2">
            <el-input v-model="model.cooperates[1].hero"></el-input>
          </el-form-item>
          <el-form-item label="最佳搭档的原因">
            <el-input v-model="model.cooperates[1].description" type="textarea"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能信息" name="skills">
          <el-button size="small" @click="model.skills.push({})">
            <i class="el-icon-plus"></i>添加技能
          </el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item,index) in model.skills" :key="index">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="冷却值">
                <el-input v-model="item.delay"></el-input>
              </el-form-item>
              <el-form-item label="消耗">
                <el-input v-model="item.cost"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL+'admin/upload'"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="res => $set(item,'icon',res.file.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input type="textarea" v-model="item.tips"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="danger" @click="model.skills.splice(index,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top: 1rem">
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: {
    id: {},
    seclists: {},
    items: {}
  },
  data() {
    return {
      model: {
        name: "",
        title: "",
        type: "",
        avatar: "",
        bgc: "",
        skills: [],
        score: {
          difficult: 0,
          skills: 0,
          attack: 0,
          survival: 0
        },
        tip: {
          usageTips: "",
          battleTips: "",
          teamTips: ""
        },
        cooperates: [
          {
            hero: "",
            description: "",
            heroid: ""
          },
          {
            hero: "",
            description: "",
            heroid: ""
          }
        ]
      },
      types: [],
      item: []
    };
  },
  methods: {
    afterUpload(res) {
      this.$set(this.model, "avatar", res.file.url);
    },
    afterUpload2(res) {
      this.$set(this.model, "bgc", res.file.url);
    },
    async save() {
      if (this.id) {
        let res = await this.$http.post(
          `admin/heroupdate/${this.id}`,
          this.model
        );
        this.$router.push("/hero/list");
        this.$message({
          type: "success",
          message: "保存成功"
        });
      } else {
        let res = await this.$http.post("admin/herocreate", this.model);
        this.$router.push("/hero/list");
        this.$message({
          type: "success",
          message: "保存成功"
        });
      }
    },
    async fetchTypes() {
      const res = await this.$http.post("admin/seclist");
      const Res = await this.$http.post("admin/itemlist");
      this.item = Res.data.data;
      const arr = res.data.data;
      this.types = arr.filter(function(item, index, array) {
        return item.toplist.name == "英雄列表";
      });
    },
    async fetch() {
      const res = await this.$http.get(`admin/herodetail/${this.id}`);
      this.model = Object.assign({}, this.model, res.data.data);
      let arr = [];
      this.seclists.forEach(item => {
        arr.push(item.sechero.secid);
      });
      this.$set(this.model, "type", arr);

      let heroitem = [];
      this.items.forEach(item => {
        heroitem.push(item.id);
      });
      this.$set(this.model, "heroitem", heroitem);
      console.log(this.model)
    }
  },
  created() {
    this.id && this.fetch();
    this.fetchTypes();
  }
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>


